#ifndef _GLSHANDER_UTIL_H_
#define _GLSHANDER_UTIL_H_

#include <GLES2/gl2.h>
#include <stdio.h>

static bool ShaderCompiledTest(GLuint shader)
{
  // Check if compilation succeeded
  GLint bShaderCompiled;
  glGetShaderiv(shader, GL_COMPILE_STATUS, &bShaderCompiled);
  if (!bShaderCompiled)
  { // failed!
    // An error happened, first retrieve the length of the log message
    int i32InfoLogLength, i32CharsWritten;
    glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &i32InfoLogLength);

    // Allocate enough space for the message and retrieve it
    char* pszInfoLog = new char[i32InfoLogLength];
    glGetShaderInfoLog(shader, i32InfoLogLength, &i32CharsWritten, pszInfoLog);
    // Displays the error in a dialog box
    printf("Failed to compile fragment shader:\n -- %s\n", pszInfoLog);
    delete[] pszInfoLog;

    return false;
  }
  return true;
}

static bool ProgramLinkedTest(GLuint program)
{
  // Check if linking succeeded in the same way we checked for compilation success
  GLint bLinked;
  glGetProgramiv(program, GL_LINK_STATUS, &bLinked);
  if (!bLinked)
  {
    int i32InfoLogLength, i32CharsWritten;
    glGetProgramiv(program, GL_INFO_LOG_LENGTH, &i32InfoLogLength);

    char* pszInfoLog = new char[i32InfoLogLength];
    glGetProgramInfoLog(program, i32InfoLogLength, &i32CharsWritten, pszInfoLog);
    printf("Failed to link program:\n -- %s\n", pszInfoLog);
    delete[] pszInfoLog;

    return false;
  }
  return true;
}

#endif  // _GLSHANDER_UTIL_H_
